'''
1、有文件“分数.txt”，求平均分。文件中的部分分数的格式有误，
不允许修改文件，用“异常处理”计算。(文件名：py0233.py)
'''
import re

f = open("分数.txt", "r")
score = 0
grades = []
grades_ = []
str_grades = " ".join(f.readlines())
print(str_grades)
grades = re.split(',|-|\n|\r| ', str_grades)
print(grades)
f.close()
aver, sum_, grades_num = 0.0, 0, 0
for i in range(len(grades)):
    try:
        score = int(grades[i])
    except:
        pass
    else:
        grades_num += 1
        sum_ += score
aver = sum_ / grades_num

print("所有分数的平均值为:{:.3f}".format(aver))
